package com.leeoohoo.config;

import com.leeoohoo.common.UserContext;
import com.leeoohoo.common.UserHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class HandlerFilter implements WebFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ThreadLocal<UserContext> current = UserHolder.getCurrent();
        System.out.println(current);
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            current.remove();
            UserHolder.clean();
            System.out.println(exchange.getResponse().getStatusCode());
        }));
    }
}
